﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace BigData
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void btn_openFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if(fbd.ShowDialog() == DialogResult.OK)
            {
                txt_folderPath.Text = fbd.SelectedPath;
            }
        }

        private void btn_ofd_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "文本文件|*.txt";
            if(ofd.ShowDialog() == DialogResult.OK)
            {
                txt_txtPath.Text = ofd.FileName;
            }
        }

        private void btn_begin_Click(object sender, EventArgs e)
        {
            sr = new StreamReader(txt_txtPath.Text);
            srLen = sr.BaseStream.Length; srLenIndex = 0;
            bdc = new BigDataContainer(txt_folderPath.Text);
            int taskLen = 5;
            Task[] t = new Task[taskLen];
            CancellationTokenSource cts = new CancellationTokenSource();
            Task.Factory.StartNew(() => {
                long rsrLenIndex = 0;
                while (true)
                {
                    Thread.Sleep(1000);
                    this.Invoke((EventHandler)delegate {
                        lab_speed.Text = $"{(srLenIndex - rsrLenIndex) / 1024 / 1024} MB/s";
                        rsrLenIndex = srLenIndex;
                    });
                    try
                    {
                        cts.Token.ThrowIfCancellationRequested();
                    }
                    catch (Exception)
                    {
                        return;
                    }


                }
            }, cts.Token);
            for (int i = 0; i < taskLen; i++)
            {
                t[i] = Task.Factory.StartNew(() => {
                    
                    while (true)
                    {
                        long len = 0;
                        List<string> list = new List<string>();
                        lock (sr)
                        {
                            for (int j = 0; j < 1000; j++)
                            {
                                if (sr.EndOfStream)
                                {
                                    if (list.Count == 0) return;
                                    break;
                                }

                                list.Add(sr.ReadLine());
                            }
                        }



                        foreach (string line in list)
                        {
                            var state = BigDataHandle.GetTextState(line);
                            switch (state)
                            {
                                case BigDataHandle.TextState.Normal:
                                    string domain = BigDataHandle.GetMailDomain(line);
                                    //Console.WriteLine(domain);
                                    bdc.WriteLine(domain, line);
                                    //Console.WriteLine(line);
                                    break;
                                case BigDataHandle.TextState.NoSeparator:
                                    bdc.WriteLine(NoSeparatorFileName, line);
                                    //Console.WriteLine(line);
                                    break;
                                case BigDataHandle.TextState.Unnormal:
                                    bdc.WriteLine(UnnormalFileName, line);
                                    break;
                            }
                            len += Encoding.Default.GetByteCount(line);
                        }

                        this.Invoke((EventHandler)delegate {
                            srLenIndex += len;
                            progressBar1.Value = Convert.ToInt32((decimal)srLenIndex / srLen * 100);
                        });
                    }
                });
            }

            Task.Factory.StartNew(() => {
                Task.WaitAll(t);
                cts.Cancel();
                this.Invoke((EventHandler)delegate {
                    progressBar1.Value = progressBar1.Maximum;
                });
                bdc.Dispose();
                sr.Close();
                sr.Dispose();
                MessageBox.Show("完成");
            });
        }
        StreamReader sr = null;
        BigDataContainer bdc = null;
        long srLen ;
        long srLenIndex ;
        const string NoSeparatorFileName = "无分隔符数据";
        const string UnnormalFileName = "杂质";

        private void FormMain_Load(object sender, EventArgs e)
        {
            //Console.WriteLine(BigDataHandle.GetTextState("guojiaqitaiyuan----guojiaqitaiyuan@163.com----welcome"));
            
        }
    }
}
